package org.finesys.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.finesys.ai.constants.EmbedConst;
import org.finesys.ai.entity.AigcDoc;
import org.finesys.ai.entity.AigcDocSlice;
import org.finesys.ai.mapper.AigcDocMapper;
import org.finesys.ai.service.AigcDocService;
import org.finesys.ai.service.AigcDocSliceService;
import org.finesys.common.core.exception.BusinessException;
import org.finesys.common.mp.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class AigcDocServiceImpl extends BaseServiceImpl<AigcDocMapper, AigcDoc> implements AigcDocService {


    private final AigcDocSliceService aigcDocSliceService;

    @Override
    public boolean addDoc(AigcDoc aigcDoc) {
        if (!StringUtils.hasText(aigcDoc.getContent())) {
            if (log.isErrorEnabled()) {
                log.error("文档内容不能为空");
            }
            throw new BusinessException("文档内容不能为空");
        }
        //知识库ID不能为空
        if (!StringUtils.hasText(aigcDoc.getKnowledgeId())) {
            if (log.isErrorEnabled()) {
                log.error("知识库ID不能为空");
            }
            throw new BusinessException("知识库ID不能为空");
        }
        aigcDoc.setType(EmbedConst.INPUT);
        aigcDoc.setSliceStatus(false);
        //保存文档信息
        boolean flag = this.save(aigcDoc);
        //保存切片信息
        if (flag) {
            try {
                //存储到向量数据库中

                //保存切片信息
                AigcDocSlice aigcDocSlice = new AigcDocSlice();
                aigcDocSlice.setDocId(aigcDoc.getId());
                aigcDocSlice.setKnowledgeId(aigcDoc.getKnowledgeId());
                aigcDocSlice.setName(aigcDoc.getName());
                aigcDocSlice.setContent(aigcDoc.getContent());
                aigcDocSlice.setVectorId(aigcDoc.getId());
                aigcDocSliceService.save(aigcDocSlice);
                //更新文档切片状态和数量
                aigcDoc.setSliceStatus(true);
                aigcDoc.setSliceNumber(1);
                this.save(aigcDoc);
            } catch (Exception e) {
                this.removeById(aigcDoc.getId());
                flag = false;
            }
        }
        return flag;
    }

    @Override
    public boolean updateDoc(AigcDoc aigcDoc) {
        //更新文档信息
        return this.updateById(aigcDoc);
    }

    /**
     * 根据文档ID数组删除文档
     *
     * @param ids 文档ID数组
     * @return 删除是否成功
     */
    @Override
    public boolean deleteDoc(String[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        List<String> idList = Arrays.asList(ids);
        //删除切片信息
        aigcDocSliceService.remove(new LambdaQueryWrapper<AigcDocSlice>().in(AigcDocSlice::getDocId, idList));
        //删除文档信息
        return super.removeBatchByIds(Arrays.asList(ids));
    }

    @Override
    public List<AigcDoc> findByKnowledgeId(String knowledgeId) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<AigcDoc>().eq(AigcDoc::getKnowledgeId, knowledgeId));
    }

}
